package com.bluetooth.lib.util;

public class HexUtil {

	
	/**
	 * @author : tangzhi
	 * @param int : mInt 整形数
	 * @return String ：4字节的16进制字符数据
	 * @description : 整形数据转换为4字节的16进制数据,高字节在前，byte[0]-高8位，byte[3]-低8位
	 * 
	 * 
	 * 
	 */
	public static String IntToByteOneHex(int mInt) {
		String localString = null;
		int localInt = mInt;
		byte localbyte[] = new byte[1];
		localbyte[0] = (byte) localInt;
		localString = bytesToHex(localbyte);
		return localString;
	}

	/**
	 * @author : tangzhi
	 * @param byte [] : 待组合字节数
	 * @return int ：返回组合后的整形数
	 * @description : 4字节的16进制数据转换为整形数据
	 * 
	 * 
	 */
	public static int ByteToIntHex(byte[] mbyte) {

		int localInt = 0;
		byte localbyte[] = new byte[4];
		System.arraycopy(mbyte, 0, localbyte, 0, mbyte.length);

		for (int k = 0; k < 4; k++) {
			int temp = 0;

			localInt &= (0xFFFFFF00);
			temp = ((int) localbyte[k]) & (0x000000FF);
			localInt |= temp;
			System.out.printf("\nlocalInt = 0x%x", localInt);

			if (k < 3)
				localInt = (localInt << (8));

			// localInt |= (((localInt << (k * 8)) & (0x00)) |
			// (((int)localbyte[k]) & (0x000000FF)));
			// localbyte[3-k] = (byte)((localInt >> (k * 8)) & (0xff));

		}

		return localInt;
	}

	private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	/**
	 * 将字节数组转换为16进制字符串
	 * 
	 * @param bcd
	 * @return
	 */

	public static final String bytesToHex(byte[] bcd) {
		StringBuffer s = new StringBuffer(bcd.length * 2);

		for (int i = 0; i < bcd.length; i++) {
			s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
			s.append(bcdLookup[bcd[i] & 0x0f]);
		}

		return s.toString();
	}

	/**
	 * 将16进制字符串转换为字节数组
	 * 
	 * @param s
	 * @return
	 */
	public static final byte[] hexToBytes(String s) {
		byte[] bytes;
		bytes = new byte[s.length() / 2];

		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
					16);
		}

		return bytes;
	}

}
